package com.example.gravitysituation;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;

public class AnimatedView extends ImageView{
	
	
	private Context mContext;

    private Handler h;
    private final int FRAME_RATE = 30;
    public Boolean running = false;
    double x;
    double y;
    double dx;
    double dy;
    
    int goal = 2000;
    double slowdown = 4.;
    public Boolean paused = false;
    public Boolean sent_alert = false;

    public AnimatedView(Context context, AttributeSet attrs)  {
            super(context, attrs);
            mContext = context;
            h = new Handler();
    }
     private Runnable r = new Runnable() {
             @Override
             public void run() {
                     invalidate();
             }
     };
     

     
     protected void onDraw(Canvas c) {
         BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.cannonball_sized);
         BitmapDrawable cannon = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.cannon_sized);
         BitmapDrawable target = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.bullseye_sized);
         if (x<0 && y <0) {
             x = this.getWidth()/2.;
             y = this.getHeight()/2.;
         } else {
             x += dx;
             y += dy;
             
             if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0) ) {
                    dx = 0; dy = 0; slowdown = 10000; 
                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setMessage("Sorry, your ball went too far");
                    builder.setCancelable(false);
                    
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int id)
                        {
                            dialog.dismiss();
                        }
                    });
                    AlertDialog alert = builder.create();
                    if (!sent_alert) {
                    	alert.show();
                    	sent_alert = true;
                    }
                    
             }
             //if ((-y > this.getHeight() - ball.getBitmap().getHeight()) ) {
             if (-y > this.getHeight() - 131  ) {
            	 y = 130 - this.getHeight();
            	 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                 if ( Math.abs(x - goal) < 20 ) {
                	 builder.setMessage("Success!");
                	 
                 } else {
                	 builder.setMessage("Your distance was: " + Double.toString(x).substring(0, 5));
                	 
                 }
                 builder.setCancelable(false);
                 
                 builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
                 {
                     public void onClick(DialogInterface dialog, int id)
                     {
                         dialog.dismiss();
                     }
                 });
                 AlertDialog alert = builder.create();
                 if (!sent_alert) {
                 	alert.show();
                 	sent_alert = true;
                 }
            	 	dx = 0; dy = 0; slowdown = 10000; paused = true;
             }
        }
        
        c.drawBitmap(target.getBitmap(), goal, this.getHeight() - target.getBitmap().getHeight()/2 - 60, null);
        if (running) {
        	int xMap = (int) Math.round(x);
        	int yMap = ball.getBitmap().getHeight() - (int) Math.round(y);
        	c.drawBitmap(ball.getBitmap(), xMap, yMap, null);
        	dy = dy - 9.8/slowdown;
        }
        c.drawBitmap(cannon.getBitmap(), 0, this.getHeight() - cannon.getBitmap().getHeight()/2 - 120, null);
        //if (!paused) {
        	h.postDelayed(r, FRAME_RATE);
        //}
        
     }
     public void setGoal(int g) {
    	 goal = g;    	 
     }
     public void Run(Double h, Double v, Double t, int g) {
    	 x = 0;
    	 y = h - (this.getHeight() - 130 ) ;
    	 slowdown = 1;

    	 paused = false;
    	 running = true;
    	 sent_alert = false;
    	 
    	 dx = Math.cos(Math.toRadians(t))*v/slowdown;
         dy = Math.sin(Math.toRadians(t))*v/slowdown;
    	 
    	 
     }

}
